/**
 * 截流函数 CP FROM PL
 * @author Li
 */
;(function (w) {

    var Throttle = function () {
        this.version = 'v1.0';
    };

    Throttle.prototype = {
        constructor: Throttle,
        requestAnimationFrame: function (fn, timer) {
            // requestAnimationFrame
            // http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/
            var _this = this,
                ww = window,
                dt = ww.document,
                _requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame,
                scrollHandler = _this.throttle(function(){
                    _requestAnimationFrame(fn);
                },timer);
            // Listening for events
            if (window.addEventListener) {
                addEventListener('scroll', scrollHandler, false);
            } else if (window.attachEvent) {
                window.attachEvent('onscroll', scrollHandler);
            } else {
                window.onscroll = scrollHandler;
            }

        },
        throttle: function (func, wait, options) {
            var _ = {
                now: Date.now || function () {
                    return new Date().getTime();
                }
            };
            var timeout, context, args, result;
            var previous = 0;
            if (!options) options = {};

            var later = function () {
                previous = options.leading === false ? 0 : _.now();
                timeout = null;
                result = func.apply(context, args);
                if (!timeout) context = args = null;
            };

            var throttled = function () {
                var now = _.now();
                if (!previous && options.leading === false) previous = now;
                var remaining = wait - (now - previous);
                context = this;
                args = arguments;
                if (remaining <= 0 || remaining > wait) {
                    if (timeout) {
                        clearTimeout(timeout);
                        timeout = null;
                    }
                    previous = now;
                    result = func.apply(context, args);
                    if (!timeout) context = args = null;
                } else if (!timeout && options.trailing !== false) {
                    timeout = setTimeout(later, remaining);
                }
                return result;
            };

            throttled.cancel = function () {
                clearTimeout(timeout);
                previous = 0;
                timeout = context = args = null;
            };

            return throttled;
        }
    };
    w.Throttle = new Throttle();
})(window);
    